package com.zim.access.common.error;

import com.zim.access.common.chat.Message;
import com.zim.access.common.chat.MessageBuilder;
import lombok.var;

/**
 * 通过该工厂类快速构造错误消息
 *
 * @author hanyue
 * @version 1.0
 * @date 2021/12/21 上午12:27
 */
public enum ErrorMessageFactory {

    INSTANCE;

    public Message getObj(Message message, ErrorMessageConstants error, Object... args) {
        return getObj(message, error.name());
    }

    public Message getObj(Message message, String error, Object... args) {
        var to = message.getFrom();
        var from = message.getTo();

        return new MessageBuilder<String>(message.getId(), message.getSeqId())
                .setContent(String.format(error, args))
                .setTo(from)
                .setFrom(to)
                .setAck(message.getAck() == null ? null : message.getAck().getTimestamp())
                .setRead(message.getRead() == null ? null : message.getRead().getTimestamp())
                .setChatType(message.getChat() == null ? null : message.getChat().getType())
                .setType(Message.Type.ERROR)
                .builder();
    }
}
